import java.util.function.Consumer;

import static java.lang.String.format;

/**
 * User: 86156
 * Date: 2023-04-17
 * Time: 16:39
 */

// 单链表
public class SinglyLikedList {
    private Node head;

    /*
     * 节点类,做成内部类,对外隐藏细节
     * */

    private static class Node {
        int value;// 节点中存的值
        Node next;// 指向下一个节点的指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    // 头增
    public void addFirst(int value) {
        // 链表为空
        // head = new Node(value,null);
        // 链表非空
        head = new Node(value, head);
    }


    // 遍历
    /*public void loop(){
        // pointer
        Node p = head;
        while(p!=null){
            System.out.println(p.value);
            p = p.next;
        }
    }*/

    public void loop(Consumer<Integer> consumer) {
        // pointer
        Node p = this.head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void addTail(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    private Node findLast() {
        if (head == null) {// 链表为空
            return null;
        }

        Node p = head;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

    private Node findIndex(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    public int get(int index){
        Node node = findIndex(index);

        if (node == null) {
            // 抛异常
            try {
                throw new IllegalAccessException(format("index[ %d ]不合法%n",index));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } else {
            return node.value;
        }
    }

    public void insertNode(int index,int value){
        if(index == 0){
            addFirst(value);
            return;
        }

        Node prev = findIndex(index-1);
        Throws(index, prev);
        prev.next = new Node(value,prev.next);
    }

    private static void Throws(int index, Node prev) {
        if (prev == null) {
            // 抛异常
            try {
                throw new IllegalAccessException(format("index[ %d ]不合法%n", index));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void removeFirst(){
        if(head == null){
            try {
                throw new IllegalAccessException(format("不合法%n"));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        head = head.next;
    }

    public void removeIndexNode(int index){
        if(index == 0){
            removeFirst();
            return;
        }
        Node prev = findIndex(index-1);
        Throws(index, prev);
        Node removed = prev.next;// 要删除的节点不存在
        Throws(index, prev);
        prev.next = removed.next;
    }
}

















